﻿namespace LogManager.Core.Models.Smtp
{
    using LogManager.Utils.Unity;
    using LogManager.Utils.Xml;
    using System.Net.Mail;
    using System.Xml.Linq;

    /// <summary>
    /// Class that represents the sender of a mail
    /// </summary>
    public class From : XmlElementBase, IFrom
    {
        public const string DEFAULT_TEMPLATE = @"<From Address='sampleFrom@domain.com' DisplayName='Admin'/>";

        public virtual string Address { get; set; }
        public virtual string DisplayName { get; set; }

        public MailAddress MailAddress
        {
            get { return new MailAddress(this.Address, this.DisplayName); }
        }

        public From(XElement baseNode)
            : base(baseNode)
        {
            this.Initialize();
            this.PropertyChanged += (a, b) => SaveChanges();
        }

        private void Initialize()
        {
            var xml = new XmlResolver<From>(BaseNode);
            this.Address = xml.GetAttributeValue(c => c.Address);
            this.DisplayName = xml.GetAttributeValue(c => c.DisplayName);
        }

        public override void SaveChanges()
        {
            var xml = new XmlResolver<From>(BaseNode);
            xml.SetAttributeValue(c => c.Address, Address);
            xml.SetAttributeValue(c => c.DisplayName, DisplayName);
        }
    }
}
